package net.ib.mtalk.ui.start;

import java.util.ArrayList;
import java.util.List;

import net.ib.mtalk.R;
import net.ib.mtalk.base.BaseListActivity;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.ui.start.StartManager.StartPreference;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

/**
 * <H1>처음 시작시에 새로운 옵션을 설정하기 위한 Activity</H1> <br/>
 * StartManager를 설정해 주면 된다. 설정하는 방법은 StartManager에서..
 * @author urunimi
 *
 */
public class StartActivity extends BaseListActivity implements OnClickListener {

	private List<StartPreference> mPrefsList = new ArrayList<StartPreference>();
	private StartPreference mCurrentPref;

	private TextView menuTitle;
	private TextView menuSummary;
	private Button buttonProceed;
	private Button buttonOk;
	private EditText editText;

	private Activity mContext;
	private SharedPreferences mPrefs;
	private int mLastListPosision;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start_activity);

		mContext = this;
		mPrefs = GlobalPreferences.getSharedPreferences();
		mPrefsList = StartManager.getInstance();
		initailizeViews();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		initailizeViews();
	}

	private void initailizeViews() {
		menuTitle = (TextView) findViewById(R.id.menuTitle);
		menuSummary = (TextView) findViewById(R.id.menuSummary);
		mLastListPosision = 0;
		buttonProceed = (Button) findViewById(R.id.buttonProceed);
		buttonProceed.setOnClickListener(this);
		buttonOk = (Button) findViewById(R.id.buttonOk);
		buttonOk.setOnClickListener(this);

		editText = (EditText) findViewById(R.id.editText);

		final ListView listView = getListView();
		listView.setItemsCanFocus(false);
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		if(mPrefsList.size() > 0) {
			mCurrentPref = mPrefsList.get(0);
			
			menuTitle.setText(mCurrentPref.getTitle());
			menuSummary.setText(mCurrentPref.getSummary());

			switch (mCurrentPref.getType()) {
			case StartManager.TYPE_LIST:
				getListView().setVisibility(View.VISIBLE);
				setListAdapter(new ArrayAdapter<String>(this,
						android.R.layout.simple_list_item_single_choice,
						getResources().getStringArray(mCurrentPref.getEntries())));
				editText.setVisibility(View.GONE);
				mLastListPosision = mCurrentPref.getDefaultPosition();
				getListView().setItemChecked(mLastListPosision, true);
				if(mLastListPosision >= 2) {
					getListView().setSelection(mLastListPosision-2);
				} else {
					getListView().setSelection(mLastListPosision);
				}
				
				break;
			case StartManager.TYPE_CHECK:
				getListView().setVisibility(View.VISIBLE);
				editText.setVisibility(View.GONE);
				setListAdapter(new ArrayAdapter<String>(this,
						android.R.layout.simple_list_item_single_choice,
						getResources().getStringArray(R.array.use_or_not)));
				mLastListPosision = mCurrentPref.getDefaultPosition();
				getListView().setItemChecked(mLastListPosision, true);
				getListView().setSelection(mLastListPosision);
				break;
			case StartManager.TYPE_EDIT:
				editText.setVisibility(View.VISIBLE);
				editText.setText(mCurrentPref.getDefaultText());
				getListView().setVisibility(View.GONE);
				break;
			default:
				break;
			}
		} else {
			finish();
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		mLastListPosision = position;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.buttonOk) {
			if (mCurrentPref != null) {
				switch (mCurrentPref.getType()) {
				//List일 경우 string insert
				case StartManager.TYPE_LIST: {
					String newValue = getResources().getStringArray(
							mCurrentPref.getEntryValues())[mLastListPosision];
					mPrefs.edit().putString(mCurrentPref.getPrefKey(), newValue).commit();
					break;
				}
				//Edit일 경우 string insert
				case StartManager.TYPE_EDIT: {
					Editable callNumber = editText.getText();
					mPrefs.edit().putString(mCurrentPref.getPrefKey(), callNumber.toString()).commit();
					break;
				}
					
				//Check일 경우 boolean insert
				case StartManager.TYPE_CHECK: {
					boolean boolValue = false;
					if (mLastListPosision == 0)
						boolValue = true;
					mPrefs.edit().putBoolean(mCurrentPref.getPrefKey(), boolValue).commit();
					break;
				}
				default:
					break;
				}
				
				//방금 선택한 옵션을 Remove한 뒤에 옵션이 전부 끝난경우 카메라 액티비티 실행
				mPrefsList.remove(0);
				if (mPrefsList.size() == 0) {
					finish();
				} else {
					Intent i = new Intent(mContext, StartActivity.class);
					startActivity(i);
				}
			} else {
				finish();
			}
		} else if (v.getId() == R.id.buttonProceed) {
			finish();
		}
	}
}
